package com.link.base.base.stocksellstorage.controller;

import com.link.core.cllog.LCLogger;
import com.link.base.base.stocksellstorage.model.StorageProd;
import com.link.base.base.stocksellstorage.service.StorageProdService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 */
@Controller
@RequestMapping(value = "/link/storageProd")
public class StorageProdController extends BasicController<StorageProd> {
    @Resource
    private StorageProdService storageProdService;

    @Override
    public BasicService<StorageProd> getBasicService() throws Exception {
        return storageProdService;
    }

    /**通过盘点的头Id查询相关的库存产品*/
    @RequestMapping(value = "/queryStorageProdsByStockId", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Map<String, Object> queryStorageProdsByStockId(StorageProd record, HttpSession session, HttpServletRequest request,
                                                          HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            // 当前记录
            int currentCount = record.getCurrentCount();
            // 总记录数
            int count = 0;
            if (record.getTotalFlag() != null && record.getTotalFlag()) {
                if (record.getCurrentCount() == 0) {
                    count = storageProdService.countStorageProdsByStockId(record);
                    result.put("count", count);
                }
            }
            record.setStartRow(currentCount + 1);
            record.setEndRow(currentCount + record.getPage());
            List<StorageProd> list = storageProdService.queryStorageProdsByStockId(record);
            result.put("result", list);
            result.put("success", true);

        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            LCLogger.withException(e);
        }
        return result;
    }
}
